<?php
defined ( 'MacTop' ) or exit ( 'Access Denied' );
header ( "Content-type: text/html; charset=utf-8" );

function GetMyConnection() {
	global $db;
	if ($db)
		return $db;
	$db = mysql_connect ( DBHOST, DBUSER, DBPASSWORD ) or die ( 'Could not connect to server.' );
	
	mysql_select_db ( DBNAME, $db ) or die ( 'Could not select database.' );
	mysql_query ( 'set names utf8;', $db );
	return $db;
}

function GetCentralConnection() {
	global $centraldb;
	if ($centraldb)
		return $centraldb;
	$centraldb = mysql_connect ( DBHOST, DBUSER, DBPASSWORD ) or die ( 'Could not connect to server.' );
	
	mysql_select_db ( DBNAME, $centraldb ) or die ( 'Could not select database.' );
	mysql_query ( 'set names utf8;', $centraldb );
	return $centraldb;
}



function GetAzureDbConnection() {
	global $azuredb;
	if ($azuredb)
		return $azuredb;
		
try {
   $azuredb = new PDO ( "sqlsrv:server = tcp:ddc1ueypgf.database.windows.net,1433; Database = yunxiaoche_db", "mactop", "Asdfgxfc123");
//   $azuredb->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}catch ( PDOException $e ) {
   print( "Error connecting to SQL Server." );
   die(print_r($e));
}
  	return $azuredb;
/*		
try {
	
	
   $azuredb = mysql_connect ( DBHOST, DBUSER, DBPASSWORD ) or die ( 'Could not connect to server.' );
   $azuredb = new PDO ( "sqlsrv:server = tcp:".AZUREDBHOST."; Database = ".AZUREDBNAME,AZUREDBUSER, AZUREDBPASSWORD);
   $azuredb->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
   
 

}catch ( PDOException $e ) {
   print( "Error connecting to SQL Server." );
   die(print_r($e));
}
*/
	/*
try {
   $conn = new PDO ( "sqlsrv:server = tcp:ddc1ueypgf.database.windows.net,1433; Database = yunxiaoche_db", "mactop", "Asdfgxfc123");
   $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}catch ( PDOException $e ) {
   print( "Error connecting to SQL Server." );
   die(print_r($e));
}
	
	*/
	
}


function CleanUpCentralDB() {
	global $centraldb;
	if ($centraldb != false)
		mysql_close ( $centraldb );
	$centraldb = false;
}



function CleanUpAzureDB() {
	global $azuredb;
	if ($azuredb != false)
		mysql_close ( $azuredb );
	$azuredb = false;
}


function CleanUpDB() {
	global $db;
	if ($db != false)
		mysql_close ( $db );
	$db = false;
}

function getIP() {
	global $_SERVER;
	if (getenv ( 'HTTP_CLIENT_IP' )) {
		$ip = getenv ( 'HTTP_CLIENT_IP' );
	} else if (getenv ( 'HTTP_X_FORWARDED_FOR' )) {
		$ip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} else if (getenv ( 'REMOTE_ADDR' )) {
		$ip = getenv ( 'REMOTE_ADDR' );
	} else {
		$ip = $_SERVER ['REMOTE_ADDR'];
	}
	return $ip;
}

// 获取一个文件夹下的文件列表 并将其存储到数组变量中 filesize
function makefilelist($folder, $filter, $sort = true, $type = "files") {
	$res = array ();
	$filter = explode ( "|", $filter ); //过滤文件名
	$temp = @opendir ( $folder );
	while ( $file = @readdir ( $temp ) ) {
		if ($type == "files" && ! in_array ( $file, $filter )) {
			if (! is_dir ( $folder . $file ))
				$res [] = $file;
		} elseif ($type == "folders" && ! in_array ( $file, $filter )) {
			if (is_dir ( $folder . $file ))
				$res [] = $file;
		}
	}
	@closedir ( $temp );
	if ($sort)
		sort ( $res );
	return $res;
}

//目录不存在就创建
function createFile($filename, $data) {
	if (! file_exists ( FileDir )) {
		@mkdir ( FileDir, 0777 );
		@system ( "chmod 0777 " . FileDir );
	}
	
	$file_name = FileDir . '/' . $filename;
	
	if (! $fp = fopen ( $file_name, "w" )) {
		throw new Exception ( '文件打开失败！' );
	}
	
	if (! fwrite ( $fp, "<?php\n return " . var_export ( $data, TRUE ) . ";\n?>" )) {
		fclose ( $fp );
		throw new Exception ( '文件写入失败！' );
	}
	
	fclose ( $fp );
	chmod ( $file_name, 0777 );
}
?>
